package org.elec.mac.service;


import org.elec.mac.page.PageList;
import org.elec.mac.page.PageProperty;
import org.elec.mac.utils.PageUtil;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.stereotype.Service;

import org.elec.mac.mapper.SFormulaMapper;
import org.elec.mac.entity.SFormula;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.DefaultTransactionDefinition;

import java.util.List;
import java.util.Map;

/**
 * @Description:实现类
 * @author 
 * @version 1.0
 * @created 
 */
@Service
public class SFormulaService extends BaseService<SFormula,SFormulaMapper> implements InitializingBean {

	@Autowired
	private SFormulaMapper sFormulaMapper;

	@Autowired
	private DataSourceTransactionManager dataSourceTransactionManager;
	
	public void afterPropertiesSet() throws Exception {
		this.setBaseMapper(sFormulaMapper);
	}

	/**
	 *
	 * @param pp
	 * @return
	 */
	public PageList<Map<String, Object>> getFormulaInfoPageList(PageProperty pp) {
		int count = sFormulaMapper.getFormulaInfoCount(pp.getParamMap());
		int start = PageUtil.getStart(pp.getNpage(), count, pp.getNpagesize());
		int end = PageUtil.getEnd(pp.getNpage(), count, pp.getNpagesize());
		pp.putParamMap("startrow", start);
		pp.putParamMap("endrow", end);
		pp.putParamMap("pagesize", pp.getNpagesize());
		PageList<Map<String,Object>> pageList = new PageList<Map<String,Object>>(pp, count, sFormulaMapper.getFormulaInfoSplitList(pp.getParamMap()));
		return pageList;
	}

	/**
	 *
	 * @param param
	 * @return
	 */
	public List<Map<String, Object>> getFormulaInfoList(Map<String, Object> param) {
		TransactionStatus transactionStatus = dataSourceTransactionManager.getTransaction(new DefaultTransactionDefinition());
		try {
			List<Map<String, Object>> list = sFormulaMapper.getFormulaInfoList(param);
			dataSourceTransactionManager.commit(transactionStatus);
			return list;
		}
		catch (Exception ex) {
			dataSourceTransactionManager.rollback(transactionStatus);
		}
		return null;
	}

	/**
	 *
	 * @param param
	 * @return
	 */
	public List<Long> getFormulaMaterialList(Map<String, Object> param) {
		return sFormulaMapper.getFormulaMaterialList(param);
	}

	public List<Map<String, Object>> getMachineMenuFormula(Map<String, Object> param) {
		return sFormulaMapper.getMachineMenuFormula(param);
	}

}
